// components/adjusting/adjusting.js
import { commonApi, storeApi } from '../../api/index';
import { getCurrentDateTime } from "../../utils/util"
// 获取app
var app = getApp();
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    selectTypeId: Number,
    selectColorId: Number,
    selectAreaId: Number,
    ran_store_id: Number,
    start_at: {
      type: String,
      value: "",
      observer(val) {
        if(val.length > 0){
          let time = getCurrentDateTime()
          // console.log('start_at', val);
          // console.log('endtime',time)
          this.setData({
            start_date: val.substring(0, 10),
            start_time: val.substring(11, 16),
            end_date: time.substring(0, 10),
            end_time: time.substring(11, 16),
          })
        }
      },
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    areaList: [],
    colorList: [],
    typeList: [],
    filesHost: app.globalData.filesHost,
    start_date: "",
    start_time: "",
    end_date: "",
    end_time: "",
  },


  /**
   * 组件的方法列表
   */
  methods: {
    async getProjectList() {
      try {
        const ran_store_id = this.data.ran_store_id
        const res = await storeApi.queryProjectList({ ran_store_id })
        let areaList = res.hair_region_list.list
        let colorList = res.hairdye_color_list.list
        let typeList = res.hairdye_type_list.list
        areaList.pop()
        colorList.pop()
        typeList.pop()
        this.setData({
          areaList: areaList,
          colorList: colorList,
          typeList: typeList,
        })
      } catch (e) { 
         console.log(e)
      }
    },

    onSelectType(e) {
      const target = e.currentTarget.dataset;
      this.triggerEvent("onSelectType", target);
    },
    onSelectColor(e) {
      const target = e.currentTarget.dataset;
      this.triggerEvent("onSelectColor", target);
    },
    onSelectArea(e) {
      const target = e.currentTarget.dataset;
      this.triggerEvent("onSelectArea", target);
    },
    startDateChange(e) {
      console.log('picker发送选择改变，携带值为', e.detail.value)
      this.setData({
       start_date: e.detail.value,
      })
    },
     startTimeChange(e) {
      console.log('picker发送选择改变，携带值为', e.detail.value)
      this.setData({
        start_time: e.detail.value,
      })
    },
    endDateChange(e) {
      console.log('picker发送选择改变，携带值为', e.detail.value)
      this.setData({
        end_date: e.detail.value,
      })
    },
    endTimeChange(e) {
      console.log('picker发送选择改变，携带值为', e.detail.value)
      this.setData({
        end_time: e.detail.value,
      })
    },
    sureComplete(){
      // console.log(1)
      const { start_date, start_time, end_date, end_time } = this.data
      this.triggerEvent("sureComplete", {
        start_at: `${start_date} ${start_time}:00`,
        finish_at: `${end_date} ${end_time}:00`
      });
    },
  },
  lifetimes: {
    attached() {
      this.getProjectList()
    }
  }
})